package num06;

public class Leecode560 {
    public int subarraySum(int[] nums, int k) {
        int length = nums.length;
        int count = 0;

        for (int num = 1; num <= length; num++) {
            if (num == 1) {
                for (int i = 0; i < length; i++) {
                    if (nums[i] == k) {
                        count++;
                    }
                }
            } else {
                int i = huaDongChuangKou(nums, num, k);
                count += i;
            }
        }

        return count;
    }

    public int huaDongChuangKou(int[] nums, int num, int k) {
        int equ = 0;

        for (int left = 0; left <= nums.length - num; left++) {
            int right = left + num - 1;
            if (equal(left, right, k, nums)) {
                equ++;
            }
        }

        return equ;
    }

    public boolean equal(int left, int right, int k, int[] nums) {
        int sum = 0;
        for (int j = left; j <= right; j++) {
            sum += nums[j];
        }
        return sum == k;
    }
}
